package com.answer.service.impl;

import com.answer.dao.AnswerDao;
import com.answer.dao.QuestionDao;
import com.answer.dto.ReturnError;
import com.answer.enums.QuestionEnum;
import com.answer.enums.ReturnInfoEnum;
import com.answer.exception.ElementEmptyException;
import com.answer.exception.RecordEmptyException;
import com.answer.lucenu.QuestionIndex;
import com.answer.model.Question;
import com.answer.service.QuestionService;
import com.answer.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/13.
 */
@Service("questionService")
public class QuestionServiceImpl implements QuestionService {
    private static final Logger log = LoggerFactory.getLogger(QuestionServiceImpl.class);

    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private AnswerDao answerDao;
    //问题索引
    private QuestionIndex questionIndex = new QuestionIndex();

    public Long countTotal(Map<String, Object> map) {
        return questionDao.countTotal(map);
    }


    public List<Question> listByPage(Map<String, Object> map) {
        return questionDao.listByPage(map);
    }

    public List<Question> listByMap(Map<String, Object> map) {
        return questionDao.listByPage(map);
    }

    public Question getById(Long id) {
        return questionDao.getById(id);
    }

    public Question getByMap(Map<String, Object> map) {
        return questionDao.getByMap(map);
    }

    @Transactional
    public ReturnError saveQues(Question ques) throws ElementEmptyException {
        try {
            if (StringUtil.isEmpty(ques.getSubject())) {
                throw new ElementEmptyException(QuestionEnum.SUBJECT.getInfo());
            }
            ques.setDelFlag(1);
            ques.setGmtCreate(new Timestamp(System.currentTimeMillis()));
            ques.setGmtModified(ques.getGmtCreate());
            questionDao.save(ques);
            questionIndex.addIndex(ques);
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
        } catch (ElementEmptyException e1) {
            throw e1;
        } catch (Exception e) {
            log.info(ReturnInfoEnum.ERROR.getInfo(), e);
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError updateQues(Question ques) throws ElementEmptyException, RecordEmptyException {
        Question question = null;
        try {
            if (StringUtil.isEmpty(ques.getSubject())) {
                throw new ElementEmptyException(QuestionEnum.SUBJECT.getInfo());
            }
            question = questionDao.getById(ques.getId());
            if (question == null) {
                //记录不存在
                throw new RecordEmptyException(ReturnInfoEnum.NORECORD.getInfo());
            }
            question.setSort(ques.getSort());
            question.setSubject(ques.getSubject());
            question.setImg(ques.getImg());
            question.setGmtModified(new Timestamp(System.currentTimeMillis()));
            questionDao.update(question);
            questionIndex.updateIndex(question);
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
        } catch (ElementEmptyException e1) {
            throw e1;
        } catch (RecordEmptyException e2) {
            throw e2;
        } catch (Exception e) {
            log.info(ReturnInfoEnum.ERROR.getInfo(), e);
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError removes(String ids) {
        String[] idarr = ids.split(",");
        try {
            for (String id : idarr) {
                Long qid = Long.parseLong(id);
                int i = questionDao.remove(qid);
                if (i != 0) {
                    questionIndex.deleteIndex("" + qid);
                    answerDao.removeByQid(qid);
                }
            }
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
        } catch (Exception e) {
            log.info(ReturnInfoEnum.ERROR.getInfo(), e);
            throw new RuntimeException(e);
        }
    }

}
